<template>
	<view>
		<view style="font-size:28rpx;line-height:20px;color: #999;padding:20px;">提示：</br> 默认未开启，0表示等比分4份：必得分红猫进度由看视频次数，邀请人数，合成猫次数、旅行团收益4部分组成， 每部分各占25%，某一部分达到25%以后不再上涨，总和达到100%可获得永久分红猫。</br>开启后，1表示满足单一条件就行：视频次数，邀请人数，合成猫次数、旅行团收益只要其中一项达到100%即可获得永久分红猫。</br>配置参数保存后，前端预计10分钟左右生效。</view>
		<view class="uni-container" style="float: left;">
			<uni-forms ref="form" validateTrigger="bind">
				<uni-forms-item label="是否满足单一条件即可获得分红猫" name="title" labelWidth="220">
					<view>
						<switch :checked="bidetype.type==1" @change="switchChange" />
					</view>
				</uni-forms-item>
				
				<view class="uni-button-group pointer">
					<button class="uni-button uni-button-full" type="primary" :disabled="isLoading" @click="save">保存</button>
				</view>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLoading:false,
				"bidetype":{
					"type":0,
				}
			}
		},
		mounted() {
			this.getinfo()
		},
		methods: {
			switchChange (e) {
				console.log('switch1 发生 change 事件，携带值为', e.target.value)
				if(e.target.value){
					this.bidetype.type = 1
				}else{
					this.bidetype.type = 0
				}
			},
			getinfo(){
				this.isLoading = true
				uni.showLoading({
					title: '加载中'
				});
				uniCloud.callFunction({
					name: 'uni-admin',
					data: {
						action: "admin/loadconfig",
						data: {
							id: 'bidetype'
						}
					}
				}).then((res) => {
					console.log(res, "get--------------")
					this.isLoading = false
					this.bidetype = res.result.config
					uni.hideLoading();
				}).catch((res) => {
					console.log(res)
				}).finally(err => {
					this.isLoading = false
					uni.hideLoading();
				})
			},
			save(){
				this.isLoading = true
				delete this.bidetype._id;
				for(let key in this.bidetype){
					if(key != '_id'){
						this.bidetype[key] = Number(this.bidetype[key])
					}
				}
				console.log(this.bidetype)
				
				uni.showLoading({
					title: '保存中'
				});
				uniCloud.callFunction({
					name: 'uni-admin',
					data: {
						action: "admin/setconfig",
						data: {
							json: JSON.stringify({
								bidetype: this.bidetype
							})
						}
					}
				}).then((res) => {
					console.log(res, "res--------------")
					this.isLoading = false
					uni.hideLoading();
					uni.showToast({
						title: "修改成功",
						icon: 'none',
						duration: 2000
					})
				}).catch((res) => {
					console.log(res)
				}).finally(err => {
					this.isLoading = false
					uni.hideLoading();
				})
			}
		}
	}
</script>

<style>

</style>
